import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/util/format_utils.dart';
import 'package:hi_common/util/logger.dart';

import '../const/Const.dart';
import '../http/model/video_model.dart';
import '../navigator/hi_navigator.dart';
import '../provider/theme_provider.dart';
import 'common_widget.dart';

class VideoDetailCard extends StatelessWidget {
  final VideoModel data;

  const VideoDetailCard(this.data, {super.key});

  @override
  Widget build(BuildContext context) {
    return _buildCard(context, data);
  }

  Widget _buildCard(BuildContext context, VideoModel data) {
    var size = MediaQuery.of(context).size;
    var width = size.width / 2 - 10;
    var height = width / (16 / 9);

    return InkWell(
      onTap: () {
        logger.i(data.url);
        HiNavigator.getInstance().onJumpTo(RouteStatus.detail,
            args: {Const.VIDEO_MODEL: data, Const.VIDEO_ID: data.vid});
      },
      child: SizedBox(
          height: height,
          child: Row(
            children: [
              _itemImage(context, data.cover, width, height),
              _buildInfo(context, data)
            ],
          )),
    );
  }

  Widget _itemImage(
      BuildContext context, String url, double width, double height) {
    return Stack(
      children: [
        ClipRRect(
            borderRadius: const BorderRadius.all(Radius.circular(4)),
            child: CachedNetworkImage(
              imageUrl: url,
              width: width,
              height: height,
              fit: BoxFit.cover,
              errorWidget: (context, url, error) => const Icon(Icons.error),
            ))
      ],
    );
  }

  Widget _buildInfo(BuildContext context, VideoModel data) {
    return Expanded(
        child: Container(
      padding: const EdgeInsets.only(top: 5, left: 8, right: 8, bottom: 5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            data.title,
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
                fontSize: 12,
                color: isDarkTheme(context) ? Colors.grey : Colors.black87),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildOwner(context, data.owner.name),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  _buildStatistic(),
                  const Icon(
                    Icons.more_vert_sharp,
                    size: 15,
                    color: Colors.grey,
                  )
                ],
              )
            ],
          )
        ],
      ),
    ));
  }

  _buildStatistic() {
    return Row(
      children: [
        ...smallIconText(Icons.ondemand_video, countFormat(data.view)),
        const Padding(padding: EdgeInsets.only(left: 5)),
        ...smallIconText(Icons.list_alt, countFormat(data.reply)),
      ],
    );
  }

  _buildOwner(BuildContext context, String nikeName) {
    return Row(
      children: [
        Container(
          padding: const EdgeInsets.all(1),
          decoration: BoxDecoration(
            border: Border.all(
                width: 1.0,
                color: Colors.grey),
            borderRadius: const BorderRadius.all(Radius.circular(2)),
          ),
          child: const Text(
            "UP",
            style: TextStyle(
                color: Colors.grey,
                fontSize: 8),
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(left: 8),
          child: Text(
            data.owner.name,
            maxLines: 1,
            style: TextStyle(
                fontSize: 11,
                color: isDarkTheme(context) ? Colors.grey : Colors.black87),
            overflow: TextOverflow.ellipsis, // 处理溢出内容
          ),
        ),
      ],
    );
  }
}
